﻿using System;
using DevExpress.Web.ASPxEditors;
using System.Data;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web.UI;
using EntryExit.Utils;
using DevExpress.Web.ASPxTreeView;
using DevExpress.Web.ASPxGridView;

namespace EntryExit
{
    public partial class WhoIsIn : BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.Master is MainMaster)
            {
                //((MainMaster)this.Master).MasterTreeViewNodeSelected += AttendanceCalendar_MasterTreeViewNodeSelected;
                ((MainMaster)this.Master).MasterTreeViewNodeSelected += AttendanceCalendar_MasterTreeViewNodeSelected;
            }
        }
        void AttendanceCalendar_MasterTreeViewNodeSelected(object source, TreeViewNodeEventArgs e)
        {

            DateTime startDate = DateTime.Now.Date.AddMonths(-3).AddDays(-20).AddHours(00).AddMinutes(00).AddSeconds(00);//..ToShortDateString() + " 00:00:00";
            DateTime endDate = DateTime.Now.Date.AddMonths(-3).AddDays(-19).AddMinutes(59).AddSeconds(59);//.ToShortDateString() + " 23:59:59";

            DataTable whoIsInData = new DataTable();

            string totalEmployees = "0";
            string totalInEmployees = "0";

            if (SelectedEmployeeLPN == null || SelectedEmployeeLPN == "")
            {

                whoIsInData = EmployeeManagerContext.GetWhoIsInInformation(CurrentEmployee.LPN, startDate, endDate, ref totalEmployees, ref totalInEmployees);
            }
            else
            {
                whoIsInData = EmployeeManagerContext.GetWhoIsInInformation(SelectedEmployeeLPN, startDate, endDate, ref totalEmployees, ref totalInEmployees);
            }

            if (whoIsInData.Rows.Count == 0)
            {
                lblInsideCount.Text = "None of your reportees are currently inside EY premises";
            }
            else
            {
                lblInsideCount.Text = string.Format("Number of reportess inside EY premises : {0}/{1}", totalInEmployees, totalEmployees);
            }

            insiderInfoGridView.PageIndex = 0;
            insiderInfoGridView.DataSource = whoIsInData;
            insiderInfoGridView.DataBind();

            insiderInfoGridView.Columns[0].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            insiderInfoGridView.Columns[1].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            insiderInfoGridView.Columns[2].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            insiderInfoGridView.Columns[3].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            insiderInfoGridView.Columns[4].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;

            insiderInfoGridView.Columns[0].CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            insiderInfoGridView.Columns[1].CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            insiderInfoGridView.Columns[2].CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            insiderInfoGridView.Columns[3].CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            insiderInfoGridView.Columns[4].CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;


            insiderInfoGridView.Visible = true;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    if (IsAdmin)
                    {
                        btnForceSync.Visible = true;
                    }

                    GetWhoAreInInformation();
                }
               
                // DisplayWhoAreIn();
            }
            catch (Exception ex)
            {
                Logger.Error("Page : WhoIsIn.aspx.cs, Event : Page_Load", ex);
                ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('An internal error occured. Reload the tool and try again. If issue still persists, please report to your administrator..!');", true);
            }
        }

        private void GetWhoAreInInformation()
        {
            DateTime startDate = DateTime.Now.Date.AddMonths(-3).AddDays(-20).AddHours(00).AddMinutes(00).AddSeconds(00);//..ToShortDateString() + " 00:00:00";
            DateTime endDate = DateTime.Now.Date.AddMonths(-3).AddDays(-19).AddMinutes(59).AddSeconds(59);//.ToShortDateString() + " 23:59:59";

            DataTable whoIsInData = new DataTable();

            string totalEmployees = "0";
            string totalInEmployees = "0";
            if (SelectedEmployeeLPN == null || SelectedEmployeeLPN == "")
            {

                whoIsInData = EmployeeManagerContext.GetWhoIsInInformation(CurrentEmployee.LPN, startDate, endDate, ref totalEmployees, ref totalInEmployees);
            }
            else
            {
                whoIsInData = EmployeeManagerContext.GetWhoIsInInformation(SelectedEmployeeLPN, startDate, endDate, ref totalEmployees, ref totalInEmployees);
            }


            if (whoIsInData.Rows.Count > 0)
            {
                insiderInfoGridView.PageIndex = 0;
                insiderInfoGridView.DataSource = whoIsInData;
                insiderInfoGridView.DataBind();

                insiderInfoGridView.Columns[0].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                insiderInfoGridView.Columns[1].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                insiderInfoGridView.Columns[2].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                insiderInfoGridView.Columns[3].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                insiderInfoGridView.Columns[4].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;

                insiderInfoGridView.Columns[0].CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                insiderInfoGridView.Columns[1].CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                insiderInfoGridView.Columns[2].CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                insiderInfoGridView.Columns[3].CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                insiderInfoGridView.Columns[4].CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;


                insiderInfoGridView.Visible = true;
                lblInsideCount.Text = string.Format("Number of reportess inside EY premises : {0}/{1}", totalInEmployees, totalEmployees);
            }
            else
            {
                lblInsideCount.Text = "None of your reportees are currently inside EY premises";
            }
        }

        private void DisplayWhoAreIn()
        {
            DataTable insiderData = new DataTable();
            insiderData.Columns.Add("Employee Name");
            insiderData.Columns.Add("Entry Gate");
            insiderData.Columns.Add("Entry Time");


            //string startDate = DateTime.Now.AddMonths(-7).ToShortDateString() + " 00:00:00";
            //DateTime startDate = DateTime.Now.Date.AddDays(-1).AddHours(00).AddMinutes(00).AddSeconds(00);//..ToShortDateString() + " 00:00:00";
            //DateTime endDate = DateTime.Now.Date.AddHours(23).AddMinutes(59).AddSeconds(59);//.ToShortDateString() + " 23:59:59";
            
                  DateTime startDate = DateTime.Now.Date.AddMonths(-3).AddDays(-20).AddHours(00).AddMinutes(00).AddSeconds(00);//..ToShortDateString() + " 00:00:00";
                  DateTime endDate = DateTime.Now.Date.AddMonths(-3).AddDays(-19).AddMinutes(59).AddSeconds(59);//.ToShortDateString() + " 23:59:59";
            

            GetSubEmployeeStatus(CurrentEmployee, startDate, endDate, ref insiderData);

            if (insiderData.Rows.Count > 0)
            {
                insiderInfoGridView.PageIndex = 0;
                insiderInfoGridView.DataSource = insiderData;
                insiderInfoGridView.DataBind();
                insiderInfoGridView.Visible = true;
                lblInsideCount.Text = string.Format("Number of reportess inside EY premises : {0}", insiderData.Rows.Count);
            }
            else
            {
                lblInsideCount.Text = "None of your reportees are currently inside EY premises";
            }
        }

        private void GetSubEmployeeStatus(Employee CurrentEmployee, DateTime startDate, DateTime endDate, ref DataTable insiderData)
        {
            IList<Employee> employeeList = EmployeeManagerContext.GetSubEmployeesForAuthorityLPN(CurrentEmployee.LPN);
            if (employeeList != null && employeeList.Count > 0)
            {
                foreach (Employee employee in employeeList)
                {
                    LocationStatus locStatus = EmployeeManagerContext.GetLatestLocationStatus(startDate, endDate, employee.CardHolderId);

                    if (locStatus != null)
                    {
                        if (locStatus.Action == "ENTRY")
                        {
                            insiderData.Rows.Add(employee.EmployeeName, locStatus.ActionGate, locStatus.LastStatusDateTime.ToShortDateString() + " " + locStatus.LastStatusDateTime.ToShortTimeString());
                        }
                    }
                }

                foreach (Employee emp in employeeList)
                {
                    GetSubEmployeeStatus(emp, startDate, endDate, ref insiderData);
                }
            }
        }
        
        protected void btnForceSync_Click(object sender, EventArgs e)
        {
            try
            {
                btnForceSync.Enabled = false;
                btnForceSync.Text = "Syncing...Please Wait";
                Process proc = new Process();
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = ConfigMgr.Instance.GetValue(ConfigKeys.CNFG_DATA_PULL_EXE_PATH);
                psi.UseShellExecute = true;
                proc.StartInfo = psi;
                proc.Start();
                proc.WaitForExit();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                btnForceSync.Enabled = true;
                btnForceSync.Text = "Force Sync";
                Response.Redirect("WhoIsIn.aspx");
            }
        }

        protected void insiderInfoGridView_PageIndexChanged(object sender, EventArgs e)
        {
            int pageIndex = (sender as ASPxGridView).PageIndex;

            insiderInfoGridView.PageIndex = pageIndex;
            this.GetWhoAreInInformation();

        }
    }
}